package com.ruoyi.web.controller.miniprogram;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.SysIdleBoatInfoDto;
import com.ruoyi.common.core.domain.entity.system.SysJishi;
import com.ruoyi.common.core.domain.entity.system.SysPintuanType;
import com.ruoyi.common.core.domain.entity.system.SysTaocan;
import com.ruoyi.common.oss.OssManager;
import com.ruoyi.system.service.ISysBoatService;
import com.ruoyi.system.service.ISysJishiService;
import com.ruoyi.system.service.ISysPintuanTypeService;
import com.ruoyi.system.service.ISysTaocanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 小程序主要业务控制器类。
 */
@Slf4j
@RestController
@RequestMapping("/miniprogram/biz")
public class MiniBizController extends BaseController {

    @Autowired
    private ISysTaocanService sysTaocanService;

    @Autowired
    private ISysPintuanTypeService sysPintuanTypeService;

    @Autowired
    private ISysJishiService sysJishiService;

    @Autowired
    private OssManager ossManager;

    @Autowired
    private ISysBoatService sysBoatService;

    @PostMapping("/selectGroupBookingBoat")
    public AjaxResult selectGroupBookingBoat() {

        return success();
    }

    /**
     * 获取包船套餐列表
     */
    @GetMapping("/getTaocanList/{scenicId}")
    public AjaxResult getTaocanList(@PathVariable("scenicId") Long scenicId) {
        SysTaocan sysTaocan = new SysTaocan();
        sysTaocan.setScenicId(scenicId);
        List<SysTaocan> result = sysTaocanService.selectSysTaocanList(sysTaocan);
        List<SysTaocan> sysTaocanList = result.stream().map(item -> {
            item.setPicture(ossManager.generatePresignedUrl(item.getPicture()).toString());
            return item;
        }).collect(Collectors.toList());
        return success(sysTaocanList);
    }

    /**
     * 获取拼团类型列表。
     *
     * @return
     */
    @GetMapping("/getPintuanTypeList/{scenicId}")
    public AjaxResult getPintuanTypeList(@PathVariable("scenicId") Long scenicId) {
        SysPintuanType sysPintuanType = new SysPintuanType();
        sysPintuanType.setScenicId(scenicId);
        List<SysPintuanType> result = sysPintuanTypeService.selectSysPintuanTypeList(sysPintuanType);
        List<SysPintuanType> sysPintuanTypes = result.stream().map(item -> {
            item.setPicture(ossManager.generatePresignedUrl(item.getPicture()).toString());
            return item;
        }).collect(Collectors.toList());
        return success(sysPintuanTypes);
    }

    /**
     * 获取计时计费规则。
     *
     * @return
     */
    @GetMapping("/getJishiRule/{scenicId}")
    public AjaxResult getJishiRule(@PathVariable("scenicId") Long scenicId) {
        SysJishi result = sysJishiService.selectSysJishiByScenicId(scenicId);
//        if (Objects.isNull(result)) return error("当前景区未设置计时计费规则");
        if (Objects.isNull(result)) {
            return success();
        }
        return success(result);
    }

    /**
     * 获取指定景区指定码头中当前所有空闲船只列表。
     *
     * @param scenicId
     * @param wharfId
     * @return
     */
    @GetMapping("/getAllIdleBoatList/{scenicId}/{wharfId}")
    public AjaxResult getAllIdleBoatList(@PathVariable("scenicId") Long scenicId, @PathVariable("wharfId") Long wharfId) {
        List<SysIdleBoatInfoDto> allIdleBoatList = sysBoatService.selectAllIdleSysBoat(scenicId, wharfId);
        return success(allIdleBoatList);
    }

    /**
     * 根据景区id，码头id和拼团类型id获取当前所有空闲的以及处于指定拼团类型下正在拼团中的船只列表。
     *
     * @param scenicId
     * @param wharfId
     * @param pintuanId
     * @return
     */
    @GetMapping("/getAllIdleAndGroupbookingBoatList/{scenicId}/{wharfId}/{pintuanId}")
    public AjaxResult getAllIdleAndGroupbookingBoatList(@PathVariable("scenicId") Long scenicId, @PathVariable("wharfId") Long wharfId, @PathVariable("pintuanId") Long pintuanId) {
        List<Map<String, Object>> result = sysBoatService.getAllIdleAndGroupbookingBoatList(scenicId, wharfId, pintuanId);
        return success(result);
    }
}
